using System;
using System.Web.UI.WebControls;
using NET.Library.Manipulation;

namespace NET.Library.Web.Forms
{
	/// <summary>
	/// Values object.
	/// </summary>
	public class Values : Strings
	{
		/// '-----------------------------------------------------------------------------------------
		/// <summary>
		/// 
		/// </summary>
		/// <param name="TextBox"></param>
		/// <returns>Returns integer value from TextBox value in a web form</returns>
		/// '-----------------------------------------------------------------------------------------
		public static int IntTextValue(System.Web.UI.WebControls.TextBox TextBox)
		{
            return (!IsEmptyString(TextBox.Text)) ? Convert.ToInt32(RemoveInvalidChar(TextBox.Text)) : 0;
		}

		/// '-----------------------------------------------------------------------------------------
		/// 'Returns string value from TextBox value in a web form
		/// '-----------------------------------------------------------------------------------------
		public static string StringTextValue(System.Web.UI.WebControls.TextBox TextBox)
		{
            return (!IsEmptyString(TextBox.Text)) ? RemoveInvalidChar(TextBox.Text) : String.Empty;
		}

        /// '-----------------------------------------------------------------------------------------
        /// 'Returns DateTime value from TextBox value in a web form
        /// '-----------------------------------------------------------------------------------------
        public static DateTime DateTimeTextValue(TextBox TextBox)
        {
            return (!IsEmptyString(TextBox.Text)) ? Convert.ToDateTime(RemoveInvalidChar(TextBox.Text)) : DateTimes.minDateTime;
        }
        
        /// '-----------------------------------------------------------------------------------------
        /// <summary>
        /// Convert TextBox text in a web form to a Single value
        /// </summary>
        /// <param name="TextBox"></param>
        /// <returns>Single</returns>
        /// '-----------------------------------------------------------------------------------------
        public static Single SingleTextValue(TextBox TextBox)
        {
            return (!IsEmptyString(TextBox.Text)) ? Convert.ToSingle(RemoveInvalidChar(TextBox.Text)) : Single.MinValue;
        }

        /// '-----------------------------------------------------------------------------------------
        /// <summary>
        /// Convert TextBox text in a web form to a decimal value
        /// </summary>
        /// <param name="TextBox"></param>
        /// <returns></returns>
        /// '-----------------------------------------------------------------------------------------
        public static decimal DecimalTextValue(TextBox TextBox)
        {
            return (!IsEmptyString(TextBox.Text)) ? Convert.ToDecimal(RemoveInvalidChar(TextBox.Text)) : decimal.Zero;
        }

		/// '-----------------------------------------------------------------------------------------
		/// 'Returns integer value from Label value in a web form
		/// '-----------------------------------------------------------------------------------------
        public static int IntTextValue(System.Web.UI.WebControls.Label Label)
		{
            return (!IsEmptyString(Label.Text)) ? Convert.ToInt32(RemoveInvalidChar(Label.Text)) : 0;
		}

		/// '-----------------------------------------------------------------------------------------
		/// 'Returns string value from Label value in a web form
		/// '-----------------------------------------------------------------------------------------
        public static string StringTextValue(Label Label)
		{
            return (!IsEmptyString(Label.Text)) ? RemoveInvalidChar(Label.Text) : String.Empty;
		}

        /// '-----------------------------------------------------------------------------------------
        /// 'Returns DateTime value from Label value in a web form
        /// '-----------------------------------------------------------------------------------------
        public static DateTime DateTimeTextValue(Label Label)
        {
            return (!IsEmptyString(Label.Text)) ? Convert.ToDateTime(RemoveInvalidChar(Label.Text)) : DateTimes.minDateTime;
        }
        /// '-----------------------------------------------------------------------------------------
        /// <summary>
        /// Convert Label text in a web form to a Single value
        /// </summary>
        /// <param name="Label"></param>
        /// <returns>Single</returns>
        /// '-----------------------------------------------------------------------------------------
        public static Single SingleTextValue(Label Label)
        {
            return (!IsEmptyString(Label.Text)) ? Convert.ToSingle(RemoveInvalidChar(Label.Text)) : Single.MinValue;
        }

        /// '-----------------------------------------------------------------------------------------
        /// <summary>
        /// Convert Label text in a web form to a decimal value
        /// </summary>
        /// <param name="Label"></param>
        /// <returns></returns>
        /// '-----------------------------------------------------------------------------------------
        public static decimal DecimalTextValue(Label Label)
        {
            return (!IsEmptyString(Label.Text)) ? Convert.ToDecimal(RemoveInvalidChar(Label.Text)) : decimal.Zero;
        }
		
        /// '-----------------------------------------------------------------------------------------
		/// 'Returns string value from Literal value in a web form
		/// '-----------------------------------------------------------------------------------------
        public static string StringTextValue(Literal Literal)
		{
            return (!IsEmptyString(Literal.Text)) ? RemoveInvalidChar(Literal.Text) : String.Empty;
		}

		/// '-----------------------------------------------------------------------------------------
		/// 'Returns integet value from Literal value in a web form
		/// '-----------------------------------------------------------------------------------------
        public static int IntTextValue(Literal Literal)
		{
            return (!IsEmptyString(Literal.Text)) ? Convert.ToInt32(RemoveInvalidChar(Literal.Text)) : 0;
		}

        /// '-----------------------------------------------------------------------------------------
        /// 'Returns DateTime value from Literal value in a web form
        /// '-----------------------------------------------------------------------------------------
        public static DateTime DatetimeTextValue(Literal Literal)
        {
            return (!IsEmptyString(Literal.Text)) ? Convert.ToDateTime(RemoveInvalidChar(Literal.Text)) : DateTimes.minDateTime;
        }
        /// '-----------------------------------------------------------------------------------------
        /// <summary>
        /// Convert Literal text in a web form to a Single value
        /// </summary>
        /// <param name="Literal"></param>
        /// <returns>Single</returns>
        /// '-----------------------------------------------------------------------------------------
        public static Single SingleTextValue(Literal Literal)
        {
            return (!IsEmptyString(Literal.Text)) ? Convert.ToSingle(RemoveInvalidChar(Literal.Text)) : Single.MinValue;
        }

        /// '-----------------------------------------------------------------------------------------
        /// <summary>
        /// Convert Literal text in a web form to a decimal value
        /// </summary>
        /// <param name="Literal"></param>
        /// <returns></returns>
        /// '-----------------------------------------------------------------------------------------
        public static decimal DecimalTextValue(Literal Literal)
        {
            return (!IsEmptyString(Literal.Text)) ? Convert.ToDecimal(RemoveInvalidChar(Literal.Text)) : decimal.Zero;
        }

		/// '-----------------------------------------------------------------------------------------
		/// 'Returns integer value for checkbox fields in a web form
		/// '-----------------------------------------------------------------------------------------
        public static int IntCheckBoxValue(CheckBox CheckBox)
		{
			return CheckBox.Checked == true ? 1 : 0;
		}

        /// '-----------------------------------------------------------------------------------------
        /// <summary>
        /// Return Y or N for checkbox fields in a web form
        /// </summary>
        /// <param name="CheckBox"></param>
        /// <returns>Y if checked, N if not.</returns>
        /// '-----------------------------------------------------------------------------------------
        public static string StringCheckBoxValue(CheckBox CheckBox)
        {
            return CheckBox.Checked == true ? "Y" : "N";
        }

		// '-----------------------------------------------------------------------------------------
		// 'Returns integer value for checkboxlist fields
		// '-----------------------------------------------------------------------------------------
		//		public static int intChkboxlsVal(CheckBoxList CheckBoxList, int Item)
		//		{
		////			foreach (int i in CheckBoxList.Items)
		////			{
		////				if CheckBoxList.Items(i).s
		////			}
		//
		//			if (CheckBoxList.Items.Count .Contains( Items.Item(Item).Selected = true))
		//			{
		//				return 1;
		//			}
		//			else
		//			{
		//				return 2;
		//			}
		//		}


		/// '-----------------------------------------------------------------------------------------
		/// 'Returns integer value from radiobuttonlist selected value in a web form
		/// '-----------------------------------------------------------------------------------------
        public static int IntRadioListValue(RadioButtonList RadioButtonList)
		{
			return RadioButtonList.SelectedItem!=null ? Convert.ToInt32(RadioButtonList.SelectedItem.Value) : 0;
		}

        /// '-----------------------------------------------------------------------------------------
        /// 'Returns string value from DropDownList selected value in a web form
        /// '-----------------------------------------------------------------------------------------
        public static string StringDropDownListValue(DropDownList DropDownList)
        {
            return DropDownList.SelectedItem != null ? DropDownList.SelectedItem.Value : String.Empty;
        }

		/// '-----------------------------------------------------------------------------------------
		/// 'Returns integer value from DropDownList selected value in a web form
		/// '-----------------------------------------------------------------------------------------
        public static int IntDropDownListValue(DropDownList DropDownList)
		{
            return DropDownList.SelectedItem != null ? Convert.ToInt32(DropDownList.SelectedItem.Value) : 0;
		}

		/// '-----------------------------------------------------------------------------------------
		/// 'Returns string value from DropDownList selected text in a web form
		/// '-----------------------------------------------------------------------------------------
		public static string StringDropDownListText(DropDownList DropDownList)
		{
			return DropDownList.SelectedItem!=null ? RemoveInvalidChar(DropDownList.SelectedItem.Text) : String.Empty;
		}

        /// -----------------------------------------------------------------------------------------
        /// <summary>
        /// Pre-select an item in a DropDownList control by a value
        /// </summary>
        /// <param name="dropDownList"></param>
        /// <param name="valueToSelect"></param>
        /// -----------------------------------------------------------------------------------------
        public static void SetDropDownListSelectedValue(DropDownList dropDownList, string valueToSelect)
        {
            for (int i = 0; i < dropDownList.Items.Count; i++)
            {
                if (dropDownList.Items[i].Value == valueToSelect)
                {
                    dropDownList.SelectedIndex = i;
                    break;
                }
            }
        }

        /// -----------------------------------------------------------------------------------------
        /// <summary>
        /// Pre-select an item in a DropDownList control by text
        /// </summary>
        /// <param name="dropDownList"></param>
        /// <param name="textToSelect"></param>
        /// -----------------------------------------------------------------------------------------
        public static void SetDropDownListSelectedText(DropDownList dropDownList, string textToSelect)
        {
            if (Validation.IsNumeric(textToSelect))
            {
                for (int i = 0; i < dropDownList.Items.Count; i++)
                {
                    if (dropDownList.Items[i].Text == textToSelect)
                    {
                        dropDownList.SelectedIndex = i;
                        break;
                    }
                }
            }
        }
	}
}
